<template>
    <div>
        <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    
    <el-form-item label="父级菜单" prop="pId">
      <el-select v-model="ruleForm.pId" placeholder="请选择父级菜单">
        <el-option  label="顶级菜单" :value="0" />
        <el-option v-for="item in menuList" :label="item.menuName" :value="item.menuId" />
      </el-select>
    </el-form-item>
    <el-form-item label="菜单名称" prop="menuName">
      <el-input v-model="ruleForm.menuName" />
    </el-form-item>
    <el-form-item label="跳转的Url地址" prop="menuUrl">
      <el-input v-model="ruleForm.menuUrl" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        确定
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>

    </div>
</template>

<script setup lang="ts">
import { reactive, ref ,onMounted} from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import {axios} from '@/axios/axiosHelp'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
   
  menuName: "",
  menuUrl: "",
  pId: null,
  orderNum: 0,
  createUser: "admin",
  updateUser: "",


})


//获取父级列表
const menuList:any = ref([])
const getMenuList = () => {
    axios.get('http://localhost:7081/api/User/QueryMenu').then((res => {
        menuList.value = res.data.data
    }))
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
        axios.post('http://localhost:7181/api/User/CreateMenu',ruleForm).then((res=>{
        if(res.data.data==1){
            ElMessage.success(res.data.msg)
            router.push('/Navigation')
        }
        if(res.data.data==0){
            ElMessage.error(res.data.msg)
        }
    }))
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}



const rules = reactive<FormRules>({
    menuName: [
    { required: true, message: '请输入菜单名称', trigger: 'blur' },
    
  ],
  pId: [
    {
      required: true,
      message: '请选择父级菜单',
      trigger: 'change',
    },
  ],
  menuUrl: [
    { required: true, message: '请输入跳转的Url地址', trigger: 'blur' },
    
  ],
})

onMounted(()=>{
    getMenuList()
})
</script>

<style scoped>

</style>